package com.db.controller;

import com.db.Dao.UserDao;
import com.db.Dao.impl.UserDaoImpl;
import com.db.entity.User;
import com.db.service.UserService;
import com.db.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerUser")
public class RegisterController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //response.setContentType("text/html;charset = utf-8");
        response.getWriter().println(request.getParameter("text"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        User user = new User(null,username,password);

        UserService us = new UserServiceImpl();

        boolean b = us.register(user);

        if( b ){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/register.jsp");

        }


    }
}
